// 用户相关类型
export interface User {
  id: string | number;
  email: string;
  nickname?: string; // 前端显示名
  username?: string; // 后端用户名
  password?: string;
  avatar?: string;
  avatar_url?: string; // 后端字段
  level?: number;
  vipStatus?: "none" | "monthly" | "yearly";
  vipExpireDate?: string;
  balance?: number;
  readingTime?: number;
  booksRead?: number;
  role?: "user" | "admin" | "moderator";
  status?: "active" | "banned" | "suspended";
  banReason?: string;
  banExpireDate?: string;
  lastLoginTime?: string;
  registerIp?: string;
  totalRecharge?: number;
  totalConsumption?: number;
  purchasedChapters?: string[]; // 已购买的章节ID列表
  createdAt?: string;
  updatedAt?: string;
  // 后端字段
  is_vip?: boolean | number;
  vip_type?: "monthly" | "yearly";
  vip_expire_date?: string;
  vip_expires_at?: string;
  coins?: number;
  created_at?: string;
  updated_at?: string;
}

// 用户操作记录类型
export interface UserOperationLog {
  id: string;
  userId: string;
  operatorId: string;
  operatorName: string;
  type:
    | "ban"
    | "unban"
    | "suspend"
    | "recharge"
    | "deduct"
    | "vip_grant"
    | "vip_revoke"
    | "level_change";
  description: string;
  oldValue?: any;
  newValue?: any;
  createdAt: string;
}

// 用户管理操作类型
export interface UserManagementAction {
  type:
    | "ban"
    | "unban"
    | "suspend"
    | "activate"
    | "recharge"
    | "deduct"
    | "grant_vip"
    | "revoke_vip"
    | "change_level"
    | "reset_password";
  reason?: string;
  duration?: number;
  amount?: number;
  vipType?: "monthly" | "yearly";
  newLevel?: number;
}

// 用户统计数据类型
export interface UserStats {
  totalUsers: number;
  totalUsersGrowth: number;
  activeUsers: number;
  activeUsersGrowth: number;
  vipUsers: number;
  vipUsersGrowth: number;
  newUsersToday: number;
  newUsersTodayGrowth: number;
}

// 小说相关类型
export interface Novel {
  id: string;
  title: string;
  author: string;
  authorId: string;
  cover: string;
  description: string;
  category: NovelCategory;
  status: NovelStatus;
  wordCount: number;
  chapterCount: number;
  rating: number;
  ratingCount: number;
  viewCount: number;
  favoriteCount: number;
  tags: string[];
  isVip: boolean;
  price?: number;
  createdAt: string;
  updatedAt: string;
  lastChapterTitle?: string;
  lastChapterTime?: string;
}

// 小说分类
export type NovelCategory =
  | "fantasy"
  | "urban"
  | "romance"
  | "historical"
  | "scifi"
  | "mystery"
  | "military"
  | "game";

// 小说状态
export type NovelStatus = "ongoing" | "completed" | "paused";

// 章节类型
export interface Chapter {
  id: string;
  novelId: string;
  title: string;
  content: string;
  wordCount: number;
  chapterNumber: number;
  isVip: boolean;
  price?: number;
  isFree: boolean;
  createdAt: string;
  updatedAt: string;
}

// 评论类型
export interface Comment {
  id: string;
  novelId: string;
  chapterId?: string;
  userId: string;
  userName: string;
  userAvatar?: string;
  content: string;
  rating?: number;
  likeCount: number;
  replyCount: number;
  createdAt: string;
  replies?: Comment[];
}

// 段落点评类型
export interface ParagraphComment {
  id: string;
  chapterId: string;
  userId: string;
  userName: string;
  userAvatar?: string;
  content: string;
  paragraphIndex: number;
  likeCount: number;
  createdAt: string;
}

// 书架类型
export interface BookshelfItem {
  id: string;
  userId: string;
  novelId: string;
  novel: Novel;
  status: "reading" | "finished" | "favorite";
  lastReadChapterId?: string;
  lastReadTime?: string;
  readProgress: number;
  createdAt: string;
  updatedAt: string;
}

// 阅读历史类型
export interface ReadingHistory {
  id: string;
  userId: string;
  novelId: string;
  chapterId: string;
  novel: Novel;
  chapter: Chapter;
  readTime: string;
  duration: number;
}

// 支付记录类型
export interface PaymentRecord {
  id: string;
  userId: string;
  type: "recharge" | "chapter" | "vip";
  amount: number;
  description: string;
  status: "pending" | "success" | "failed";
  paymentMethod?: "alipay" | "wechat" | "bank";
  createdAt: string;
}

// 充值套餐类型
export interface RechargePackage {
  id: string;
  amount: number;
  bonus: number;
  price: number;
  originalPrice?: number;
  isPopular?: boolean;
  description?: string;
}

// 钱包密码相关类型
export interface WalletPassword {
  userId: string;
  hasPassword: boolean;
  createdAt?: string;
  updatedAt?: string;
}

// VIP套餐类型
export interface VipPackage {
  id: string;
  name: string;
  duration: number; // 天数
  price: number;
  originalPrice: number;
  benefits: string[];
  isPopular?: boolean;
}

// 推荐算法相关类型
export interface RecommendationItem {
  novel: Novel;
  score: number;
  reason: string;
  type: "similar" | "popular" | "new" | "category";
}

// API响应类型
export interface ApiResponse<T = any> {
  code: number;
  message: string;
  data: T;
}

// 分页类型
export interface Pagination {
  page: number;
  pageSize: number;
  total: number;
  totalPages: number;
}

export interface PaginatedResponse<T> {
  items: T[];
  pagination: Pagination;
}

// 搜索类型
export interface SearchFilters {
  category?: NovelCategory;
  status?: NovelStatus;
  wordCountMin?: number;
  wordCountMax?: number;
  isVip?: boolean;
  tags?: string[];
  sortBy?: "rating" | "wordCount" | "updatedAt" | "viewCount";
  sortOrder?: "asc" | "desc";
}

// 阅读器设置类型
export interface ReaderSettings {
  fontSize: number;
  lineHeight: number;
  backgroundColor: string;
  textColor: string;
  fontFamily: string;
  pageWidth: number;
  nightMode: boolean;
}

// 登录表单类型
export interface LoginForm {
  email: string;
  password: string;
  remember: boolean;
}

// 注册表单类型
export interface RegisterForm {
  email: string;
  password: string;
  confirmPassword: string;
  nickname: string;
  agree: boolean;
}

// 忘记密码表单类型
export interface ForgotPasswordForm {
  email: string;
}

// 重置密码表单类型
export interface ResetPasswordForm {
  email: string;
  verificationCode: string;
  newPassword: string;
  confirmPassword: string;
}

// 验证码响应类型
export interface VerificationCodeResponse {
  success: boolean;
  message: string;
  code?: string; // 仅在开发环境下返回，用于测试
}

// 用户状态类型
export interface UserState {
  user: User | null;
  isLoggedIn: boolean;
  token: string | null;
  bookshelf: BookshelfItem[];
  readingHistory: ReadingHistory[];
  readerSettings: ReaderSettings;
  walletPassword: WalletPassword | null;
  paymentRecords: PaymentRecord[];
  _initialized: boolean; // 初始化标志
  _authListenerAdded: boolean; // 监听器标志
}

// 应用状态类型
export interface AppState {
  theme: "light" | "dark";
  locale: "zh-CN" | "en-US";
  loading: boolean;
}
